package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.MVShowTypeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Classname ShowTypeAttrLinkHandler
 * @Description 产品属性 初始不展示 ，点击更多展示，show_type=M
 *              此时属性联动由隐藏变为显示时，默认show_type=D，需要将 show_type=M
 *              显示由数据库sql配置实现，此处仅修改 show_type=M
 *
 *     注：该类目前仅修改 show_type=M，实际展示/隐藏、必填/非必填 由表配置实现
 * @Author zhang.song
 * @Date 2021-02-22 16:35
 */
@Service
@AttrHandler(MVShowTypeAttrHandler.class)
public class MVShowTypeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String fieldName = attr.getFieldName();
        switch(fieldName) {
            case "2_0000047":
                //IP协议版本和路由协议确定内网地址/掩码是否必填
                KiteAttr attr_200003778_1 = inst.getAttr("200003778");
                KiteAttr attr_200003779_1 = inst.getAttr("200003779");
                KiteAttr attr_2_0000042 = inst.getAttr("2_0000042");
                if("1_0000219".equals(attr.getValue())) {
                    // ipv4 隐藏ipv6相关属性
                    LocalAttrHelper.hideAndNullable(inst, "2_0000184", "2_0000185","200003779");
                    LocalAttrHelper.showAndNotNull(inst, "2_0000163", "2_0000167", "200003778");
                }
                else if ("1_0000220".equals(attr.getValue())) {
                    // ipv6 隐藏ipv4相关属性
                    LocalAttrHelper.showAndNotNull(inst, "2_0000184", "2_0000185","200003779");
                    LocalAttrHelper.hideAndNullable(inst, "2_0000163", "2_0000167", "200003778");
                }
                else if ("1_0000221".equals(attr.getValue())) {
                    // 双栈ipv4、ipv6相关属性均展示且必填
                    LocalAttrHelper.showAndNotNull(inst, "2_0000184", "2_0000185","200003779");
                    LocalAttrHelper.showAndNotNull(inst, "2_0000163", "2_0000167", "200003778");
                }
                if(Objects.nonNull(attr_2_0000042)){
                    //ipV4
                    if(Objects.nonNull(attr_200003778_1)&&KiteStringUtils.equalsAny(attr.getValue(),"1_0000219","1_0000221")&&!KiteStringUtils.equals(attr_2_0000042.getValue(),"1_0000213")) {
                        attr_200003778_1.setIsNull(KeyConsts.IFTRUE_F);
                    } else {
                        attr_200003778_1.setIsNull(KeyConsts.IFTRUE_T);
                    }
                    //ipV6
                    if(Objects.nonNull(attr_200003779_1)&&KiteStringUtils.equalsAny(attr.getValue(),"1_0000220","1_0000221")&&!KiteStringUtils.equals(attr_2_0000042.getValue(),"1_0000213")){
                        attr_200003779_1.setIsNull(KeyConsts.IFTRUE_F);
                    }else {
                        attr_200003779_1.setIsNull(KeyConsts.IFTRUE_T);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
